<?php

/**
 * 函数库
 * ============================================================================
 * Power By Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: Func.class.php 48 2011-07-18 09:57:45Z zwt007 $
 * http://www.t-mac.org；
 */
class Func
{

    /**
     * 替换url链接
     */
    static function replace_for_mod_rewrite()
    {
        //取config配置文件
        $configcache = Tmac::config('configcache.config', 'admin', '.inc.php');

        if (empty($configcache['cfg_rewrite'])) {
            return;
        }
        $cfg_indexurl = $configcache['cfg_indexurl'];
        $content = ob_get_contents();
        $urlin = array(
            "/index.php\?m=(\w+)\.(\w+)&username=(\w+)&cid=([0-9]+)&page=([0-9]+)/",
            "/index.php\?m=(\w+)&username=(\w+)&cid=([0-9]+)&page=([0-9]+)/",            
            "/index.php\?m=(\w+)\.(\w+)&username=(\w+)&(cid|aid)=([0-9]+)/",
            "/index.php\?m=(\w+)&username=(\w+)&(cid|aid)=([0-9]+)/",            
            "/index.php\?m=index\.(\w+)&cid=([0-9]+)/",
            "/index.php\?m=(\w+)\.(\w+)&username=(\w+)/",
            "/index.php\?m=difang&username=(\w+)/",
            "/index.php\?m=(\w+)&username=(\w+)/",
        );
        $urlout = array(
            "{$cfg_indexurl}$3/$1-$2-cid-$4-$5.html",
            "{$cfg_indexurl}$2/$1-cid-$3-$4.html",                    
            "{$cfg_indexurl}$3/$1-$2-$4-$5.html",
            "{$cfg_indexurl}$2/$1-$3-$4.html",                    
            "{$cfg_indexurl}$1-$2.html",
            "{$cfg_indexurl}$3/$1-$2.html",
            "{$cfg_indexurl}$1/",
            "{$cfg_indexurl}$2/$1.html",
        );
        $s = preg_replace($urlin, $urlout, $content);
        ob_end_clean();
        //上面clean后再次开启压缩
        if ($GLOBALS['TmacConfig']['Common']['gzip']) {
            function_exists('ob_gzhandler') ? ob_start('ob_gzhandler') : ob_start();
        } else {
            ob_start();
        }

        echo $s;
    }

}